﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics.Eventing.Reader;

namespace Common
{
    public class lLogWrite
    {
        public static bool isLog = true;
        public static void WriteLog(string strFileName, string strLog)
        {
            try
            {
                File.AppendAllText(strFileName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + strLog + "\r\n", Encoding.Default);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        public static void WriteLog(string strLog)
        {
            if (isLog)
            {
                string strFileName = lIniOperate.IniAppPath() + DateTime.Now.ToString("yyMMdd") + "Net.Log";
                lLogWrite.WriteLog(strFileName, strLog);               
            }
           
        }
        public static void WriteLogbyTopic(string topic,string strLog)
        {
            if (isLog)
            {
                char[] TrimChar = { ' ', '-', '\'', '\"', '\\' };       //此处使用了转义字符如：\',\",\\，分别表示单引号，双引号，反斜杠
                if (topic.Contains("/"))
                {
                    string[] parts = topic.Split('/');
                    topic = parts[0];
                }                
                string strFileName = lIniOperate.IniAppPath() + DateTime.Now.ToString("yyMMdd") + String.Format("{0}.Log",topic);
                lLogWrite.WriteLog(strFileName, strLog);
            }

        }
        public static void WriteTopicLog(string topic, string strLog)
        {
            if (isLog)
            {
                char[] TrimChar = { ' ', '-', '\'', '\"', '\\' };       //此处使用了转义字符如：\',\",\\，分别表示单引号，双引号，反斜杠
                if (topic.Contains("/"))
                {
                    string[] parts = topic.Split('/');
                    topic = parts[0];
                }
                string strFileName = lIniOperate.IniAppPath() +"Log\\" +topic+"\\"+DateTime.Now.ToString("yyMMdd") + String.Format("{0}.Log", "Net");
                string dir=Path.GetDirectoryName(strFileName);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                lLogWrite.WriteLog(strFileName, strLog);
            }

        }

    }
}
